home *** CD-ROM | disk | FTP | other *** search
- Path: druid.borland.com!usenet
- From: pete@borland.com (Pete Becker)
- Newsgroups: comp.lang.c++
- Subject: Re: aborting a constructor
- Date: 6 Feb 1996 20:47:02 GMT
- Organization: Borland International
- Message-ID: <4f8eo6$kik@druid.borland.com>
- References: <310DA3AB.6E32@tribeca.ios.com>
- NNTP-Posting-Host: pbecker.borland.com
- Mime-Version: 1.0
- Content-Type: Text/Plain; charset=ISO-8859-1
- X-Newsreader: WinVN 0.99.5
-
- In article <310DA3AB.6E32@tribeca.ios.com>, leonardj@tribeca.ios.com says...
- >
- >In an earlier post, I asked how you use 'new' and a constructor in order to
- deal with the
- >following situation:
- > You have a class, let's call it WindowClass, which, when it is
- instantiated, does the
- >following:
- > 1: It opens the file "filename", and gets its size.
- > 2: It allocates memory to hold "filename".
- > 3: It loads "filename" into that memory.
- > 4: It opens a window to display "filename".
- > Now let's suppose that after it performs steps 1 thru 3 it cannot open
- the window, what does it
- >do? Furthermore, how do you code for this kind of situation in C++ in
- general? My first thought was
- >that the constructor should de-allocate all of the resources that it had
- received so far. Then it
- >should throw an exception. The question that I had was: what's going to
- happen to the memory that
- >was allocated for the object's data members?
-
- The compiler is responsible for generating code that will release allocated
- memory when a constructor throws an exception.
-
- >I nother words: what happens to an object if its
- >constructor aborts?
-
- This is an entirely different question, and its answer depends on what you
- mean by "aborts".
-
- > Is it automatically deleted? I still haven't really gotten to the bottom of
- that
- >question but I want to pose a possible solution to this dilemma and see if it
- works.
- > Instead of the course of action described above, let's say that you have
- the constructor
- >execute the following statements:
- >
- > delete this;
- > throw(error);
- >
- >and you have the destructor of the class de-allocate the objects. I'd like to
- know if this would
- >work.
-
- It would be a disaster. Think about what happens if you try to create one of
- these things on the stack and the constructor fails.
- -- Pete
-
-